package palms.messages;

import java.io.IOException;
import java.net.Socket;

import javax.crypto.SealedObject;

import palms.exceptions.InvalidClassMessage;
import palms.exceptions.InvalidTicketMessage;
import palms.exceptions.InvalidUserMessage;
import palms.security.PalmsSecurityManager;

public class SLRequest extends PMessage {

	private static final long serialVersionUID = -2670031931686615370L;
	
	private SealedObject slticket;
	private SealedObject serviceinfo;
	
	public SLRequest(SealedObject slticket, SealedObject serviceinfo) {
		this.slticket = slticket;
		this.serviceinfo = serviceinfo;
	}
	public SLRequest() {
	}
	public SealedObject getSlticket() {
		return slticket;
	}
	public void setSlticket(SealedObject slticket) {
		this.slticket = slticket;
	}
	public SealedObject getServiceinfo() {
		return serviceinfo;
	}
	public void setServiceinfo(SealedObject serviceinfo) {
		this.serviceinfo = serviceinfo;
	}
	
	public void sendTroughSocket(Socket s) throws IOException{
		PalmsSecurityManager.sendObjectTroughtSocket(s, new SLRequest());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getSlticket());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getServiceinfo());
	}
	
	public static SLRequest receiveTroughSocket(Socket s) throws IOException, ClassNotFoundException, InvalidClassMessage, InvalidUserMessage, InvalidTicketMessage{
		Object o = PalmsSecurityManager.receiveObjectTroughSocket(s);
		if(!(o instanceof SLRequest))
			PalmsSecurityManager.throwCommException(o);
		else{
			SLRequest sr = (SLRequest)o;
			SealedObject ticket = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			SealedObject sinfo = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			sr.setSlticket(ticket);
			sr.setServiceinfo(sinfo);
			return sr;
		}
		return null;
	}
	
	
}
